# -*- coding: utf-8 -*-
# @Time:2021/07/30 23:36
# @Author:Cindy

from jenkinsapi.jenkins import Jenkins


class ExecuteTools:
    # jenkins的服务地址
    BASE_URL = "http://192.168.1.108:8081/"
    # 用户名
    USERNAME = "admin"
    # 使用Jenkins生成的token
    PASSWORD = "11521db626d7f7f2175fda001d2da00f43"
    JOB_NAME = 'ck18'
    @classmethod
    def get_jobs(cls):
        # 实例化Jenkins对象
        jenkins = Jenkins(cls.BASE_URL, cls.USERNAME, cls.PASSWORD)
        # 获取Jenkins的Item列表
        return jenkins.keys()

    @classmethod
    def invoke(cls, task):
        jenkins = Jenkins(cls.BASE_URL, cls.USERNAME, cls.PASSWORD)
        job = jenkins.get_job(cls.JOB_NAME)
        job.invoke(build_params={'task': task})
        number = job.get_last_buildnumber()

        while True:
            # 获取的不一定是最后一次构建的number
            build_number = job.get_last_buildnumber()
            if build_number != number:
                # 拼接allure report路径
                report_path = cls.BASE_URL+'job/'+cls.JOB_NAME+'/'+\
                              str(build_number)+'/allure/'
                return report_path



